<?php
namespace app\wechat\controller;

require ROOT_PATH . '/../extend/taobao/TopSdk.php';
class Tbk extends \think\Controller
{
    public function index() {
        //$content = 'https://detail.tmall.com/item.htm?spm=a219t.11816977.0.defcc5d99.3ad775a5rkmLCb&id=544721331065&scm=null&pvid=100_11.178.147.65_35529_3941561961169037057&app_pvid=59590_11.1.218.201_425_1561961169031&ptl=floorId:20311;pvid:100_11.178.147.65_35529_3941561961169037057;app_pvid:59590_11.1.218.201_425_1561961169031&union_lens=lensId:0b01dac9_0edb_16bac240148_04c6';
        //$content = 'https://detail.tmall.com/item.htm?id=590569242575&ali_refid=a3_430583_1006:1103041296:N:BaxGxtmXhnUjBqGrANoKiA==:fffaee776d9eff77467dd8dd9af7c486&ali_trackid=1_fffaee776d9eff77467dd8dd9af7c486&spm=a230r.1.14.1';


        $c = new \TopClient;
        $c->appkey = '24576928';
        $c->secretKey = 'fb0fc6e4915ade5b96130be12d4b4986';
        $c->format = 'json';


        $content = 'https://detail.tmall.com/item.htm?spm=a1z10.3-b.w4011-2879666162.184.51c96637KNUZtk&id=595556166661&rn=06b1fb21224bb0ee33b7fefd67e36c8b&abbucket=1';

        //$content = '【儿童液晶手写板小黑板家用非磁性无尘涂鸦绘画画板宝宝电子写字板】https://m.tb.cn/h.eTTS8IW?sm=2b45b4 点击链接，再选择浏览器咑閞；或復·制这段描述￥MpLoYgreEiO￥后到👉淘♂寳♀👈';
        if (preg_match('/￥.*￥/', $content)) {
            $req = new \WirelessShareTpwdQueryRequest;
            $req->setPasswordContent($content);
            $resp = $c->execute($req);

            $title = $resp->content;
            $logo = $resp->thumb_pic_url;
            $content = $resp->url;
        }


        if (!preg_match('/^https?:\/\/.*tmall\.com.+/', $content) && !preg_match('/^https?:\/\/.*taobao\.com.+/', $content)) {
            return false;
        }

        $str = parse_url($content);
        $query = convertUrlQuery($str['query']);
        if (!isset($query['id']) || empty($query['id']) || !is_numeric($query['id'])) {
            return false;
        }
        $item_id = $query['id'];

        $req = new \TbkCouponConvertRequest;
        $req->setItemId($item_id);
        $req->setAdzoneId("124464845");
        $req->setPlatform("2");
        $resp = $c->execute($req);
        if (isset($resp->result->results->coupon_remain_count) && $resp->result->results->coupon_remain_count > 0)  {
            $coupon_click_url = $resp->result->results->coupon_click_url;
        }
        $item_url = $resp->result->results->item_url;

        if (empty($title) || empty($logo)) {
            $req = new \TbkItemInfoGetRequest;
            $req->setNumIids($item_id);
            $resp = $c->execute($req);

            $title = $resp->results->n_tbk_item[0]->title;
            $logo = $resp->results->n_tbk_item[0]->small_images->string[0];
        }


        if (isset($coupon_click_url) && !empty($coupon_click_url)) {
            // 优惠券 淘口令
            $req = new \TbkTpwdCreateRequest;
            $req->setText($title);
            $req->setUrl($coupon_click_url);
            $req->setLogo($logo);
            $req->setExt("{}");
            $resp = $c->execute($req);
            $tpwd = $resp->data->model;
            $url = $coupon_click_url;
        } else {
            // 下单 淘口令
            $req = new \TbkTpwdCreateRequest;
            $req->setText( $title);
            $req->setUrl($item_url);
            $req->setLogo($logo);
            $req->setExt("{}");
            $resp = $c->execute($req);
            $tpwd = $resp->data->model;
            $url = $item_url;
        }


        // 转换成短链

        echo $tpwd = " {$title} \n 复制这条信息，{$tpwd}，到【手机淘宝】即可查看 \n ----------------- \n 【下单链接】（ {$url} ） ";
    }


    public function tpwd() {
        $c = new \TopClient;
        $c->appkey = '24576928';
        $c->secretKey = 'fb0fc6e4915ade5b96130be12d4b4986';
        $c->format = 'json';

        $req = new \WirelessShareTpwdQueryRequest;
        $req->setPasswordContent("￥sJ7iYgJyxJ4￥");
        $resp = $c->execute($req);
        var_dump($resp);
    }
}
